Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 610)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.02144 13.01833 13.01529 13.01232 13.00942 13.00656 13.00376 13.00101
##   [9] 12.99829 12.99561 12.99295 12.99032 12.98770 12.98510 12.98251 12.97991
##  [17] 12.97731 12.97470 12.97207 12.96943 12.96675 12.96405 12.96130 12.95851
##  [25] 12.95568 12.95278 12.94983 12.94682 12.94373 12.94058 12.93740 12.93419
##  [33] 12.93095 12.92769 12.92441 12.92112 12.91781 12.91450 12.91119 12.90788
##  [41] 12.90458 12.90130 12.89802 12.89477 12.89155 12.88835 12.88518 12.88206
##  [49] 12.87898 12.87594 12.87295 12.87002 12.86715 12.86435 12.86161 12.85894
##  [57] 12.85636 12.85385 12.85143 12.84910 12.84686 12.84472 12.84269 12.84076
##  [65] 12.83883 12.83679 12.83463 12.83237 12.83001 12.82756 12.82502 12.82241
##  [73] 12.81972 12.81696 12.81415 12.81127 12.80835 12.80538 12.80238 12.79934
##  [81] 12.79628 12.79320 12.79011 12.78701 12.78391 12.78081 12.77772 12.77465
##  [89] 12.77161 12.76859 12.76560 12.76266 12.75977 12.75692 12.75414 12.75142
##  [97] 12.74877 12.74620 12.74371 12.74132 12.73901 12.73681 12.73471 12.73273
## [105] 12.73087 12.72913 12.72752 12.72605 12.72473 12.72355 12.72253 12.72167
## [113] 12.72098 12.72046 12.72012 12.71996 12.72000 12.72023 12.72067 12.72132
## [121] 12.72218 12.72326 12.72458 12.72612 12.72791 12.72994 12.73222 12.73476
## [129] 12.73831 12.74350 12.75016 12.75811 12.76719 12.77722 12.78802 12.79943
## [137] 12.81127 12.82337 12.83556 12.84766 12.85950 12.87091 12.88172 12.89175
## [145] 12.90083 12.90879 12.91545 12.92308 12.93377 12.94709 12.96260 12.97989
## [153] 12.99851 13.01804 13.03806 13.05813 13.07781 13.09670 13.11434 13.13032
## [161] 13.14420 13.15556 13.16397 13.17103 13.17863 13.18670 13.19521 13.20410
## [169] 13.21334 13.22286 13.23263 13.24260 13.25272 13.26294 13.27321 13.28349
## [177] 13.29374 13.30389 13.31392 13.32376 13.33337 13.34270 13.35172 13.36036
## [185] 13.36858 13.37634 13.38359 13.39027 13.39635 13.40177 13.40650 13.41047
## [193] 13.41365 13.41598 13.41742 13.41792 13.41744 13.41592 13.41332 13.40960
## [201] 13.40470 13.39857 13.39118 13.38247 13.37240 13.36001 13.34459 13.32645
## [209] 13.30589 13.28321 13.25871 13.23270 13.20548 13.17735 13.14861 13.11958
## [217] 13.09055 13.06182 13.03369 13.00648 12.98048 12.95600 12.93334 12.91279
## [225] 12.89088 12.86435 12.83384 12.80000 12.76349 12.72496 12.68506 12.64443
## [233] 12.60374 12.56363 12.52475 12.48775 12.45329 12.42201 12.39457 12.37162
## [241] 12.35142 12.33180 12.31275 12.29426 12.27631 12.25888 12.24197 12.22556
## [249] 12.20964 12.19419 12.17920 12.16465 12.15054 12.13685 12.12356 12.11066
## [257] 12.09814 12.08599 12.07418 12.06271 12.05157 12.04074 12.03020 12.01994
## [265] 12.00996 12.00023 11.99074 11.98148 11.97243 11.96359 11.95493 11.94645
## [273] 11.93812 11.93036 11.92351 11.91753 11.91233 11.90785 11.90403 11.90081
## [281] 11.89810 11.89586 11.89401 11.89249 11.89122 11.89016 11.88922 11.88834
## [289] 11.88746 11.88651 11.88542 11.88413 11.88257 11.88068 11.87839 11.87564
## [297] 11.87235 11.86846 11.86391 11.85862 11.85254 11.84560 11.83773 11.82858
## [305] 11.81790 11.80581 11.79240 11.77779 11.76207 11.74535 11.72774 11.70934
## [313] 11.69025 11.67059 11.65045 11.62993 11.60916 11.58822 11.56722 11.54627
## [321] 11.52548 11.50494 11.48476 11.46505 11.44591 11.42745 11.40977 11.39297
## [329] 11.37717 11.36246 11.34895 11.33674 11.32594 11.31666 11.30899 11.30305
## [337] 11.29894 11.29524 11.29066 11.28544 11.27981 11.27403 11.26833 11.26296
## [345] 11.25816 11.25418 11.25126 11.24964 11.24957 11.25129 11.25505 11.26108
## [353] 11.26970 11.28091 11.29450 11.31025 11.32795 11.34739 11.36836 11.39063
## [361] 11.41400 11.43826 11.46318 11.48857 11.51419 11.53985 11.56532 11.59040
## [369] 11.61486 11.63850 11.66110 11.68246 11.70436 11.72865 11.75516 11.78374
## [377] 11.81423 11.84646 11.88029 11.91555 11.95207 11.98971 12.02830 12.06769
## [385] 12.10771 12.14820 12.18901 12.22998 12.27094 12.31175 12.35223 12.39224
## [393] 12.43160 12.47017 12.50778 12.54427 12.57949 12.61327 12.64546 12.67590
## [401] 12.70442 12.73087 12.75509 12.77693 12.79621 12.81279 12.82801 12.84326
## [409] 12.85844 12.87343 12.88815 12.90247 12.91629 12.92950 12.94201 12.95370
## [417] 12.96447 12.97422 12.98283 12.99019 12.99622 13.00079 13.00292 13.00189
## [425] 12.99800 12.99156 12.98289 12.97228 12.96004 12.94648 12.93190 12.91662
## [433] 12.90094 12.88516 12.86959 12.85455 12.84032 12.82723 12.81558 12.80567
## [441] 12.79782 12.79015 12.78066 12.76949 12.75676 12.74260 12.72713 12.71049
## [449] 12.69279 12.67416 12.65473 12.63462 12.61397 12.59290 12.57152 12.54998
## [457] 12.52840 12.50690 12.48560 12.46464 12.44415 12.42424 12.40504 12.38668
## [465] 12.36929 12.35300 12.33792 12.32418 12.31192 12.30125 12.29231 12.28385
## [473] 12.27465 12.26482 12.25451 12.24384 12.23295 12.22197 12.21103 12.20026
## [481] 12.18979 12.17976 12.17029 12.16152 12.15358 12.14661 12.14072 12.13606
## [489] 12.13276 12.13095 12.13061 12.13156 12.13368 12.13685 12.14094 12.14582
## [497] 12.15138 12.15749 12.16402 12.17086 12.17787 12.18494 12.19194 12.19874
## [505] 12.20523 12.21127 12.21784 12.22593 12.23544 12.24629 12.25839 12.27164
## [513] 12.28596 12.30125 12.31742 12.33439 12.35205 12.37033 12.38913 12.40835
## [521] 12.42792 12.44773 12.46771 12.48775 12.50776 12.52766 12.54736 12.56676
## [529] 12.58577 12.60431 12.62227 12.63958 12.65615 12.67187 12.68666 12.70043
## [537] 12.71309 12.72454 12.73470 12.74348 12.75079 12.75653 12.76061 12.76295
## [545] 12.76345 12.76202 12.75944 12.75654 12.75330 12.74971 12.74573 12.74137
## [553] 12.73660 12.73140 12.72575 12.71964 12.71305 12.70596 12.69836 12.69023
## [561] 12.68154 12.67228 12.66244 12.65200 12.64093 12.62923 12.61687 12.60384
## [569] 12.59011 12.57568 12.56051 12.54461 12.52794 12.51050 12.49225 12.47320
## [577] 12.45331 12.43257 12.41096 12.38847 12.36507 12.34084 12.31586 12.29012
## [585] 12.26364 12.23641 12.20843 12.17971 12.15026 12.12007 12.08915 12.05749
## [593] 12.02511 11.99201 11.95818 11.92364 11.88838 11.85241 11.81572 11.77833
## [601] 11.74023 11.70144 11.66194 11.62175 11.58086 11.53928 11.49701 11.45406
## [609] 11.41043 11.36612
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 610)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.62519 12.62070 12.61632 12.61204 12.60788 12.60381 12.59985 12.59597
##   [9] 12.59219 12.58850 12.58489 12.58136 12.57791 12.57453 12.57122 12.56798
##  [17] 12.56480 12.56167 12.55861 12.55560 12.55263 12.54971 12.54683 12.54399
##  [25] 12.54119 12.53841 12.53567 12.53294 12.53024 12.52755 12.52489 12.52226
##  [33] 12.51965 12.51708 12.51455 12.51206 12.50961 12.50721 12.50487 12.50258
##  [41] 12.50035 12.49819 12.49610 12.49408 12.49213 12.49026 12.48848 12.48678
##  [49] 12.48518 12.48367 12.48225 12.48094 12.47974 12.47865 12.47767 12.47680
##  [57] 12.47606 12.47544 12.47496 12.47460 12.47438 12.47430 12.47437 12.47458
##  [65] 12.47493 12.47539 12.47597 12.47667 12.47748 12.47839 12.47943 12.48057
##  [73] 12.48182 12.48318 12.48464 12.48621 12.48789 12.48967 12.49156 12.49354
##  [81] 12.49563 12.49781 12.50010 12.50248 12.50495 12.50753 12.51019 12.51296
##  [89] 12.51581 12.51875 12.52179 12.52491 12.52812 12.53142 12.53480 12.53827
##  [97] 12.54182 12.54545 12.54917 12.55296 12.55684 12.56079 12.56482 12.56893
## [105] 12.57311 12.57737 12.58170 12.58610 12.59057 12.59511 12.59972 12.60440
## [113] 12.60914 12.61395 12.61882 12.62376 12.62876 12.63382 12.63894 12.64412
## [121] 12.64936 12.65465 12.66001 12.66541 12.67087 12.67638 12.68225 12.68873
## [129] 12.69575 12.70322 12.71106 12.71920 12.72755 12.73604 12.74458 12.75310
## [137] 12.76152 12.76976 12.77774 12.78538 12.79260 12.79932 12.80732 12.81821
## [145] 12.83167 12.84736 12.86496 12.88414 12.90457 12.92592 12.94787 12.97009
## [153] 12.99225 13.01402 13.03507 13.05508 13.07372 13.09065 13.10556 13.11811
## [161] 13.12798 13.13655 13.14543 13.15456 13.16392 13.17345 13.18313 13.19290
## [169] 13.20274 13.21261 13.22246 13.23226 13.24196 13.25153 13.26092 13.27011
## [177] 13.27905 13.28769 13.29601 13.30396 13.31151 13.31861 13.32522 13.33131
## [185] 13.33684 13.34176 13.34604 13.34964 13.35252 13.35464 13.35597 13.35645
## [193] 13.35606 13.35476 13.35250 13.34924 13.34495 13.33959 13.33312 13.32549
## [201] 13.31668 13.30664 13.29533 13.28271 13.26683 13.24623 13.22158 13.19356
## [209] 13.16283 13.13007 13.09596 13.06117 13.02638 12.99226 12.95949 12.92874
## [217] 12.90069 12.87601 12.85537 12.83514 12.81149 12.78483 12.75557 12.72412
## [225] 12.69089 12.65630 12.62074 12.58464 12.54839 12.51242 12.47712 12.44291
## [233] 12.41020 12.37940 12.35092 12.32516 12.30255 12.28348 12.26669 12.25064
## [241] 12.23530 12.22065 12.20667 12.19332 12.18060 12.16847 12.15692 12.14592
## [249] 12.13544 12.12547 12.11598 12.10695 12.09836 12.09018 12.08239 12.07497
## [257] 12.06789 12.06114 12.05468 12.04850 12.04257 12.03687 12.03137 12.02607
## [265] 12.02092 12.01591 12.01101 12.00621 12.00330 12.00384 12.00743 12.01366
## [273] 12.02214 12.03247 12.04425 12.05708 12.07056 12.08429 12.09787 12.11090
## [281] 12.12297 12.13370 12.14269 12.14952 12.15380 12.15514 12.15313 12.14948
## [289] 12.14612 12.14297 12.13996 12.13702 12.13407 12.13103 12.12783 12.12440
## [297] 12.12066 12.11653 12.11195 12.10683 12.10111 12.09470 12.08754 12.07891
## [305] 12.06822 12.05562 12.04124 12.02520 12.00764 11.98869 11.96849 11.94715
## [313] 11.92482 11.90162 11.87769 11.85316 11.82815 11.80281 11.77726 11.75164
## [321] 11.72607 11.70069 11.67563 11.65101 11.62698 11.60366 11.58118 11.55968
## [329] 11.53929 11.52014 11.50235 11.48607 11.47142 11.45854 11.44755 11.43859
## [337] 11.43179 11.42549 11.41814 11.41001 11.40137 11.39250 11.38366 11.37512
## [345] 11.36717 11.36007 11.35409 11.34950 11.34659 11.34561 11.34684 11.35056
## [353] 11.35695 11.36590 11.37719 11.39061 11.40596 11.42301 11.44156 11.46139
## [361] 11.48231 11.50408 11.52652 11.54939 11.57250 11.59563 11.61857 11.64111
## [369] 11.66304 11.68414 11.70422 11.72305 11.74227 11.76359 11.78686 11.81196
## [377] 11.83874 11.86706 11.89680 11.92782 11.95997 11.99313 12.02717 12.06193
## [385] 12.09730 12.13312 12.16928 12.20562 12.24202 12.27835 12.31445 12.35020
## [393] 12.38547 12.42011 12.45400 12.48698 12.51894 12.54974 12.57923 12.60728
## [401] 12.63376 12.65854 12.68147 12.70241 12.72125 12.73783 12.75375 12.77055
## [409] 12.78803 12.80596 12.82415 12.84238 12.86044 12.87812 12.89520 12.91149
## [417] 12.92676 12.94081 12.95342 12.96439 12.97350 12.98055 12.98559 12.98891
## [425] 12.99062 12.99085 12.98972 12.98736 12.98388 12.97940 12.97405 12.96795
## [433] 12.96122 12.95399 12.94637 12.93849 12.93046 12.92242 12.91447 12.90676
## [441] 12.89938 12.89093 12.87999 12.86675 12.85138 12.83405 12.81493 12.79419
## [449] 12.77202 12.74857 12.72403 12.69857 12.67236 12.64557 12.61838 12.59096
## [457] 12.56348 12.53611 12.50903 12.48242 12.45643 12.43126 12.40706 12.38402
## [465] 12.36230 12.34208 12.32353 12.30683 12.29214 12.27965 12.26951 12.25980
## [473] 12.24860 12.23615 12.22267 12.20838 12.19352 12.17831 12.16297 12.14774
## [481] 12.13285 12.11851 12.10496 12.09242 12.08113 12.07130 12.06317 12.05695
## [489] 12.05289 12.05121 12.05130 12.05237 12.05436 12.05720 12.06081 12.06514
## [497] 12.07010 12.07564 12.08169 12.08817 12.09502 12.10217 12.10955 12.11709
## [505] 12.12473 12.13240 12.14126 12.15246 12.16585 12.18131 12.19871 12.21791
## [513] 12.23879 12.26121 12.28505 12.31017 12.33643 12.36372 12.39189 12.42083
## [521] 12.45038 12.48044 12.51086 12.54151 12.57226 12.60298 12.63355 12.66382
## [529] 12.69367 12.72296 12.75158 12.77937 12.80622 12.83200 12.85656 12.87979
## [537] 12.90154 12.92169 12.94011 12.95667 12.97123 12.98366 12.99384 13.00163
## [545] 13.00690 13.00952 13.01059 13.01128 13.01156 13.01139 13.01076 13.00963
## [553] 13.00798 13.00578 13.00299 12.99959 12.99556 12.99085 12.98546 12.97934
## [561] 12.97246 12.96481 12.95635 12.94705 12.93689 12.92583 12.91385 12.90092
## [569] 12.88701 12.87209 12.85613 12.83912 12.82101 12.80177 12.78139 12.75983
## [577] 12.73706 12.71306 12.68780 12.66124 12.63337 12.60430 12.57420 12.54307
## [585] 12.51092 12.47774 12.44354 12.40833 12.37210 12.33486 12.29662 12.25739
## [593] 12.21715 12.17592 12.13370 12.09050 12.04631 12.00115 11.95501 11.90790
## [601] 11.85983 11.81079 11.76079 11.70984 11.65793 11.60508 11.55129 11.49655
## [609] 11.44088 11.38427
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 610)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.01983 12.01510 12.01050 12.00600 12.00161 11.99732 11.99313 11.98902
##   [9] 11.98499 11.98104 11.97716 11.97335 11.96959 11.96589 11.96223 11.95861
##  [17] 11.95502 11.95146 11.94793 11.94441 11.94090 11.93739 11.93388 11.93037
##  [25] 11.92684 11.92329 11.91971 11.91610 11.91246 11.90877 11.90503 11.90123
##  [33] 11.89737 11.89345 11.88945 11.88537 11.88124 11.87707 11.87290 11.86870
##  [41] 11.86451 11.86031 11.85612 11.85194 11.84779 11.84366 11.83956 11.83550
##  [49] 11.83148 11.82752 11.82361 11.81977 11.81600 11.81231 11.80870 11.80517
##  [57] 11.80175 11.79842 11.79521 11.79211 11.78913 11.78628 11.78356 11.78098
##  [65] 11.77855 11.77628 11.77416 11.77221 11.77043 11.76883 11.76741 11.76603
##  [73] 11.76453 11.76293 11.76124 11.75946 11.75762 11.75571 11.75374 11.75174
##  [81] 11.74970 11.74765 11.74558 11.74351 11.74145 11.73941 11.73740 11.73543
##  [89] 11.73351 11.73165 11.72986 11.72816 11.72654 11.72503 11.72363 11.72235
##  [97] 11.72120 11.72020 11.71934 11.71866 11.71814 11.71781 11.71767 11.71774
## [105] 11.71802 11.71853 11.71927 11.72025 11.72150 11.72300 11.72479 11.72686
## [113] 11.72922 11.73189 11.73488 11.73820 11.74185 11.74586 11.75022 11.75494
## [121] 11.76005 11.76555 11.77144 11.77774 11.78447 11.79162 11.79921 11.80725
## [129] 11.81691 11.82915 11.84371 11.86032 11.87871 11.89860 11.91972 11.94182
## [137] 11.96461 11.98782 12.01119 12.03444 12.05731 12.07952 12.10080 12.12088
## [145] 12.13950 12.15638 12.17126 12.18740 12.20787 12.23207 12.25938 12.28920
## [153] 12.32092 12.35394 12.38766 12.42145 12.45473 12.48688 12.51730 12.54537
## [161] 12.57050 12.59208 12.60950 12.62480 12.64039 12.65623 12.67226 12.68844
## [169] 12.70472 12.72106 12.73741 12.75372 12.76994 12.78603 12.80194 12.81762
## [177] 12.83303 12.84811 12.86283 12.87713 12.89097 12.90429 12.91706 12.92923
## [185] 12.94074 12.95156 12.96163 12.97091 12.97934 12.98689 12.99351 12.99914
## [193] 13.00375 13.00728 13.00969 13.01092 13.01095 13.00970 13.00715 13.00323
## [201] 12.99792 12.99115 12.98287 12.97306 12.96165 12.94685 12.92726 12.90339
## [209] 12.87577 12.84491 12.81133 12.77555 12.73809 12.69947 12.66020 12.62082
## [217] 12.58183 12.54376 12.50712 12.47243 12.44022 12.41100 12.38530 12.36362
## [225] 12.34195 12.31630 12.28724 12.25536 12.22123 12.18542 12.14852 12.11109
## [233] 12.07373 12.03700 12.00148 11.96775 11.93639 11.90797 11.88307 11.86227
## [241] 11.84409 11.82665 11.80993 11.79390 11.77853 11.76380 11.74967 11.73612
## [249] 11.72313 11.71067 11.69871 11.68721 11.67617 11.66554 11.65531 11.64544
## [257] 11.63590 11.62668 11.61774 11.60905 11.60060 11.59235 11.58427 11.57634
## [265] 11.56853 11.56081 11.55317 11.54556 11.53796 11.53035 11.52270 11.51497
## [273] 11.50716 11.49982 11.49351 11.48814 11.48367 11.48000 11.47707 11.47481
## [281] 11.47315 11.47202 11.47134 11.47105 11.47107 11.47134 11.47178 11.47232
## [289] 11.47289 11.47342 11.47383 11.47407 11.47405 11.47370 11.47296 11.47176
## [297] 11.47001 11.46766 11.46463 11.46085 11.45624 11.45074 11.44428 11.43636
## [305] 11.42660 11.41514 11.40210 11.38761 11.37179 11.35478 11.33669 11.31766
## [313] 11.29781 11.27727 11.25616 11.23462 11.21276 11.19072 11.16862 11.14660
## [321] 11.12476 11.10325 11.08219 11.06170 11.04192 11.02296 11.00496 10.98805
## [329] 10.97234 10.95797 10.94506 10.93374 10.92413 10.91637 10.91058 10.90689
## [337] 10.90542 10.90588 10.90785 10.91119 10.91577 10.92149 10.92821 10.93581
## [345] 10.94417 10.95317 10.96268 10.97258 10.98275 10.99307 11.00341 11.01365
## [353] 11.02523 11.03951 11.05623 11.07516 11.09604 11.11863 11.14269 11.16796
## [361] 11.19421 11.22118 11.24864 11.27632 11.30399 11.33141 11.35832 11.38448
## [369] 11.40964 11.43356 11.45598 11.47668 11.49713 11.51893 11.54200 11.56622
## [377] 11.59151 11.61776 11.64489 11.67280 11.70138 11.73055 11.76021 11.79026
## [385] 11.82061 11.85116 11.88181 11.91247 11.94304 11.97342 12.00353 12.03326
## [393] 12.06252 12.09121 12.11923 12.14650 12.17290 12.19836 12.22276 12.24603
## [401] 12.26805 12.28873 12.30798 12.32570 12.34180 12.35617 12.37005 12.38460
## [409] 12.39965 12.41505 12.43062 12.44619 12.46159 12.47666 12.49122 12.50512
## [417] 12.51817 12.53022 12.54110 12.55063 12.55864 12.56498 12.56936 12.57174
## [425] 12.57231 12.57122 12.56868 12.56485 12.55990 12.55403 12.54740 12.54020
## [433] 12.53260 12.52478 12.51692 12.50919 12.50178 12.49486 12.48861 12.48321
## [441] 12.47884 12.47428 12.46829 12.46094 12.45234 12.44257 12.43174 12.41992
## [449] 12.40722 12.39373 12.37953 12.36473 12.34940 12.33366 12.31758 12.30127
## [457] 12.28481 12.26829 12.25182 12.23547 12.21935 12.20355 12.18815 12.17326
## [465] 12.15896 12.14534 12.13251 12.12054 12.10954 12.09960 12.09080 12.08178
## [473] 12.07123 12.05938 12.04641 12.03256 12.01802 12.00301 11.98774 11.97241
## [481] 11.95725 11.94245 11.92823 11.91480 11.90237 11.89115 11.88135 11.87318
## [489] 11.86686 11.86258 11.85926 11.85575 11.85213 11.84849 11.84494 11.84157
## [497] 11.83847 11.83573 11.83346 11.83175 11.83069 11.83038 11.83091 11.83237
## [505] 11.83487 11.83850 11.84361 11.85047 11.85897 11.86902 11.88053 11.89340
## [513] 11.90754 11.92286 11.93926 11.95665 11.97494 11.99403 12.01383 12.03424
## [521] 12.05517 12.07654 12.09824 12.12017 12.14226 12.16440 12.18651 12.20848
## [529] 12.23022 12.25165 12.27266 12.29316 12.31307 12.33228 12.35070 12.36824
## [537] 12.38481 12.40031 12.41465 12.42774 12.43947 12.44977 12.45853 12.46566
## [545] 12.47107 12.47466 12.47726 12.47975 12.48210 12.48429 12.48628 12.48805
## [553] 12.48958 12.49084 12.49180 12.49243 12.49272 12.49263 12.49214 12.49122
## [561] 12.48985 12.48799 12.48563 12.48274 12.47929 12.47525 12.47060 12.46531
## [569] 12.45936 12.45272 12.44537 12.43727 12.42840 12.41874 12.40826 12.39693
## [577] 12.38473 12.37163 12.35760 12.34263 12.32667 12.30984 12.29227 12.27396
## [585] 12.25491 12.23513 12.21462 12.19339 12.17143 12.14876 12.12537 12.10127
## [593] 12.07646 12.05095 12.02475 11.99784 11.97024 11.94196 11.91299 11.88334
## [601] 11.85301 11.82201 11.79034 11.75801 11.72501 11.69135 11.65704 11.62208
## [609] 11.58647 11.55021
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")